package ajakuulauku;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

/**
 * Main menu, contains level list and some buttons
 * Dialog, so main program waits for user input
 * Because its dialog its not shows in taskbar
 * 
 * @author Valdar
 *
 */
@SuppressWarnings("serial")
public class MainMenu extends JDialog implements ActionListener {
	
	/**
	 * Possible choices that can be made in menu
	 * 
	 * @author Valdar
	 *
	 */
	public static enum UserChoice {
		Play, //New game
		Edit, //Edit level
		Create, //Create level
		Cancel //Default, menu was probably just closed
	}
	
	/**
	 * What user did in menu
	 */
	UserChoice choice = UserChoice.Cancel;
	
	/**
	 * index of selected level in level list
	 */
	private int levelIndex;
	
	/**
	 * JList that is used for selecting level
	 * property, so we could later get index of selected level
	 */
	private JList levelivalik;
	
	/**
	 * Create menu
	 * 
	 * @param levelList array of level names for list
	 */
	public MainMenu(String[] levelList) {
		setTitle("Aja kuul auku!!!!! - Main Menu");
		setModal(true);
		build(levelList);
		
	}
	
	/**
	 * Build menu
	 * 
	 * @param levelList array of level names for list
	 */
	private void build(String[] levelList) {
		JPanel panel = new JPanel();
		//create components
		JScrollPane jScrollPane1 = new JScrollPane();
		levelivalik = new JList(levelList);
		levelivalik.setName("levelivalik");
		
		JButton jButton1 = new JButton("Play selected level");
		jButton1.addActionListener(this);
        
        JButton jButton2 = new JButton("Edit selected level");
        jButton2.addActionListener(this);
        
        JButton jButton3 = new JButton("Create a new level");
        jButton3.addActionListener(this);
        jScrollPane1.setViewportView(levelivalik);
        
        JLabel listTitle = new JLabel("Levelid");

        //Create Grouplayout and add components
        //Generated in Netbeans IDE
        GroupLayout mainPanelLayout = new javax.swing.GroupLayout(panel);
        panel.setLayout(mainPanelLayout);
        mainPanelLayout.setHorizontalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                		.addComponent(listTitle, 200, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                		.addComponent(jScrollPane1, 200, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jButton1, 250, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, 250, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton3, 250, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(257, Short.MAX_VALUE))
        );
        mainPanelLayout.setVerticalGroup(
            mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(mainPanelLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(mainPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(mainPanelLayout.createSequentialGroup()
                    	.addGap(18)
                    	.addComponent(jButton1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton3))
                    .addGroup(mainPanelLayout.createSequentialGroup()
                        .addComponent(listTitle, 18, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(jScrollPane1, 400, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(48, Short.MAX_VALUE))
        );
        setContentPane(panel);
	}
	
	/**
	 * Prepares and shows menu
	 * @return users choice
	 */
	public UserChoice init() {
		pack();
		//resize(GUI.WIDTH, GUI.HEIGHT);
		setVisible(true);
		return choice;
	}

	/**
	 * Action listener for buttons
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		//If play level button pressed and level selected send level index to engine and start level
		if(e.getActionCommand() == "Play selected level") {
			choice = UserChoice.Play;
			levelIndex = levelivalik.getSelectedIndex();
			if(levelIndex < 0) {
				JOptionPane.showMessageDialog(this, "Choose level to play it.");
				return;
			}
		}
		//If edit level button pressed start level editor and give it level index
		if(e.getActionCommand() == "Edit selected level") {
			choice = UserChoice.Edit;
			levelIndex = levelivalik.getSelectedIndex();
			if(levelIndex < 0) {
				JOptionPane.showMessageDialog(this, "Choose level to edit it.");
				return;
			}
		}
		//if create level button pressed start editor
		if(e.getActionCommand() == "Create a new level") {
			choice = UserChoice.Create;
		}
		setVisible(false);
	}

	/**
	 * Getter for levelIndex
	 * 
	 * @return index of selected level
	 */
	public int getLevelIndex() {
		return levelIndex;
	}

}
